package com.icss.head.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.icss.common.servlet.BaseServlet;
import com.icss.common.util.PageInfoUtil;
import com.icss.common.vo.ApiResponse;
import com.icss.service.IProductDbService;
import com.icss.service.IUsersDbService;
import com.icss.service.impl.ProductDbServiceImpl;
import com.icss.service.impl.UsersDbServiceImpl;
import com.icss.vo.MakeupBlog;
import com.icss.vo.MakeupBlogEnum;
import com.icss.vo.MakeupCategory;
import com.icss.vo.MakeupCategoryEnum;
import com.icss.vo.MakeupProduct;
import com.icss.vo.MakeupProductEnum;
import com.icss.vo.MakeupUsers;

/**
 *  首页操作
 * 
 * @author 陈岩
 */
@WebServlet(urlPatterns = "/IndexHeadServlet")
public class IndexHeadServlet extends BaseServlet
{
	private IProductDbService productDbService ;
	private IUsersDbService usersDbService ;
	
	@Override
	public void init(ServletConfig config) throws ServletException
	{
		super.init(config);
		this.productDbService = new ProductDbServiceImpl();
		this.usersDbService = new UsersDbServiceImpl();
	}
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/* 接收参数 */
		String method = request.getParameter("method");
		if ("main".equalsIgnoreCase(method))
		{
			/* main */
			main(request, response);
		} else if ("header".equalsIgnoreCase(method))
		{
			/* header */
			header(request, response);
		}else if ("productList".equalsIgnoreCase(method))
		{
			/* productList */
			productList(request, response);
		} else if ("productInfo".equalsIgnoreCase(method))
		{
			/* productInfo */
			productInfo(request, response);
		}  else if ("about".equalsIgnoreCase(method))
		{
			/* about */
			about(request, response);
		} else if ("blogList".equalsIgnoreCase(method))
		{
			/* blogList */
			blogList(request, response);
		}  else if ("blogUpdate".equalsIgnoreCase(method))
		{
			/* blogUpdate */
			blogUpdate(request, response);
		} 
	}
	
	/**
	 * 首页
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	public void main(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		pageInfoUtil.setPageSize(12);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		/* 必须是上架的商品 */
		condMap.put("status", MakeupProductEnum.STATUS_UP.getStatus());
		/* 按照销量排名的前20件商品 */
		request.setAttribute("orderby", "saleNumDesc");
		
		/* 扩展信息 */
		condMap.put("extend", "true");
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<MakeupProduct> apiResponse = this.productDbService.findCondListProductService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		/* 页面跳转 */
		String path = this.prefix + "/head/main" + this.subfix; 
		request.getRequestDispatcher(path).forward(request, response);
	}
	
	/**
	 * header
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	public void header(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 查询所有的分类 */
		/* 分页信息 */
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("status", MakeupCategoryEnum.STATUS_ENABLE.getStatus());
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<MakeupCategory> apiResponse = this.productDbService.findCondListCateService(null, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("headerResponse", apiResponse.toJSON());
		String path = this.prefix + "/head/header" + this.subfix; 
		request.getRequestDispatcher(path).include(request, response);
	}
	
	/**
	 * 商品列表
	 * @param request
	 * @param response
	 * @return 
	 * @throws ServletException
	 * @throws IOException
	 */
	public void productList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		pageInfoUtil.setPageSize(12);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		/* 必须是上架的商品 */
		condMap.put("status", MakeupProductEnum.STATUS_UP.getStatus());
		
		/* 根据分类搜索 */
		String categoryId = request.getParameter("categoryId");
		if(categoryId == null)
		{
			categoryId = "" ; 
		}
		request.setAttribute("categoryId", categoryId);
		condMap.put("categoryId", categoryId);

		/* 根据dataType搜索 */
		String dataType = request.getParameter("dataType");
		if(dataType == null)
		{
			dataType = "" ; 
		}
		request.setAttribute("dataType", dataType);
		condMap.put("dataType", dataType);
		
		/* 根据分类搜索 */
		String orderby = request.getParameter("orderby");
		if(orderby == null)
		{
			orderby = "" ; 
		}
		request.setAttribute("orderby", orderby);
		condMap.put("orderby", orderby);
		condMap.put("extend", "true");
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<MakeupProduct> apiResponse = this.productDbService.findCondListProductService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 查询所有的分类 */
		condMap.clear();
		condMap.put("status", MakeupCategoryEnum.STATUS_ENABLE.getStatus());
		ApiResponse<MakeupCategory> cateApiResponse = this.productDbService.findCondListCateService(null, condMap);
		request.setAttribute("cateResponse", cateApiResponse.toJSON());
		
		/* 页面跳转 */
		String path = "/head/productList" ; 
		path = this.prefix + path + this.subfix; 
		request.getRequestDispatcher(path).forward(request, response);
	}
	
	/**
	 * 商品列表
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void productInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("id", id);
		ApiResponse<MakeupProduct> apiResponse = this.productDbService.findOneProductService(condMap);
		request.setAttribute("response", apiResponse.toJSON());
		/* 页面跳转 */
		String path = "/head/productInfo" ; 
		path = this.prefix + path + this.subfix; 
		request.getRequestDispatcher(path).forward(request, response);
	}
	
	/**
	 * 关于我们
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void about(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 页面跳转 */
		String path = "/head/about" ; 
		path = this.prefix + path + this.subfix; 
		request.getRequestDispatcher(path).forward(request, response);
	}
	
	/**
	 * 博客列表
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void blogList(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
	{
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		/* 必须是上架的商品 */
		condMap.put("status", MakeupBlogEnum.STATUS_ENABLE.getStatus());
		
		String operType = request.getParameter("operType");
		request.setAttribute("operType", operType);
		if("self".equalsIgnoreCase(operType))
		{
			/* 只查询自己 */
			MakeupUsers users = (MakeupUsers) this.findObjfromSession(request, "users");
			
			if(users == null)
			{
				/* 请先登陆 */
				response.sendRedirect(request.getContextPath() + "/NoLoginServlet?method=login");
				return ; 
			}
			
			/* 查询我的,查询启用的购物车 */
			condMap.put("usersId", users.getId());
		}
		
		/* 根据分类搜索 */
		String orderBy = request.getParameter("orderBy");
		if(orderBy == null)
		{
			orderBy = "" ; 
		}
		request.setAttribute("orderBy", orderBy);
		condMap.put("orderBy", orderBy);
		condMap.put("extend", "true");
		
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<MakeupBlog> apiResponse = this.usersDbService.findCondListBlogService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 页面跳转 */
		String path = this.prefix + "/head/blogList" + this.subfix; 
		request.getRequestDispatcher(path).forward(request, response);
	}

	/**
	 * 博客更新
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void blogUpdate(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
	{
		String path = "/head/blogInfo" ; 
		/* 准备路径 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("id", id);
		condMap.put("extend", "true");
		ApiResponse<MakeupBlog> apiResponse = this.usersDbService.findOneBlogService(condMap);
		request.setAttribute("response", apiResponse.toJSON());
		
		String operType = request.getParameter("operType");
		if("update".equalsIgnoreCase(operType))
		{
			/* 博客更新 */
			path = "/head/users/blogUpdate" ;
		}
		
		/* 页面跳转 */
		path = this.prefix + path + this.subfix; 
		request.getRequestDispatcher(path).forward(request, response);
	}
}
